<template>
    <el-dialog ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="50%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="选择人员" prop="staffId">
                <el-select v-model="ruleForm.staffId" class="w100p" clearable filterable placeholder="请选择人员"
                    @change="handleChangeStaffType">
                    <el-option v-for="item, index in personList" :key="index" :label="item.staffName"
                        :value="item.staffId"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="类型" prop="type">
                <el-select v-model="ruleForm.type" class="w100p" clearable filterable placeholder="请选择类型">
                    <el-option  label="场长" value="FARMER"></el-option>
                    <el-option  label="检验员" value="INSPECTOR"></el-option>
                    <el-option  label="技术员" value="TECHNICIAN"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import inspectorAssignment from '@/api/quality-management/inspectorAssignment.js';
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        personList: {
            type: Array,
            default: () => {
                return []
            }
        },
    },
    data() {
        return {
            addEditName: '添加',
            addEditApi: inspectorAssignment.add,
            ruleForm: {},
            rules: {
                staffId: [
                    { required: true, message: '请选择人员', trigger: 'change' }
                ],
                type: [
                    { required: true, message: '请选择类型', trigger: 'change' }
                ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() { },
    methods: {
        handleChangeStaffType() {
            let rut =  this.personList.find(item=>{
                return item.staffId==this.ruleForm.staffId
            })
            let staffName = rut.staffName||''
            this.$set(this.ruleForm, 'staffName', staffName)
            console.log(staffName)
        },
        initData() {
            if (this.addEditFlag == 'add') {
                this.addEditApi = inspectorAssignment.add
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = {
                    ...this.data,
                    staffId: this.data.personNo||this.data.staffId
                }
                this.addEditApi = inspectorAssignment.edit
                this.addEditName = '编辑'
            }
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        let msg = res.success ? '操作成功' : '操作失败'
                        this.$message[res.success ? 'success' : 'warning'](res.msg || msg)
                        res.success && this.cancel();
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>